

clear
set more off
use "Flynn Fordham II Replication Data Main Models.dta"

* Predicted Probability - Negative Evals

	nbreg hosteval mfgwt cong_dummy2-cong_dummy12, robust
	
	sum mfgwt if e(sample)
	local min - r(min)
	local max = r(max) 
	local interval = ((`max'-`min')/10)
	margins, at(mfgwt=(.05(.01).50) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
	
	#delimit ;
		marginsplot, xlabel(.05(.05).50) recastci(rarea) recast(line) 
		ci1opts(color(gs14) fintensity(100) yaxis(2))   
		plot1opts(lwidth(.50) yaxis(2)) 
		xlabel(, format(%9.2fc)) 
		xdimension(mfgwt)
		ylabel()
		ytitle("Predicted Probability",axis(2))
		legend(off)
		title("")
		subtitle("(a)" "Negative Evaluations")
		aspect(1)
		addplot(kdensity mfgwt if mfgwt >= .04 & mfgwt <= .55, xlab(.05(.05).50, format(%9.2fc)) color(gs10) lpattern(dash) yaxis(1) yscale(alt off)
		|| function y = 0.00, range(.05 .52) lwidth(.25) yaxis(2)
		)
		saving(pr_neg, replace)
		;
		#delimit cr

		
* Predicted Probability - Positive Evals

	nbreg faveval mfgwt cong_dummy2-cong_dummy12, robust
	
	sum mfgwt if e(sample)
	local min - r(min)
	local max = r(max) 
	local interval = ((`max'-`min')/10)
	margins, at(mfgwt=(.05(.01).50) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
	

	#delimit ;
		marginsplot, xlabel(.05(.05).50) recastci(rarea) recast(line) 
		ci1opts(color(gs14) fintensity(100) yaxis(2))   
		plot1opts(lwidth(.50) yaxis(2)) 
		xlabel(, format(%9.2fc)) 
		xdimension(mfgwt)
		ylabel()
		ytitle("Predicted Probability",axis(2))
		legend(off)
		title("")
		subtitle("(b)" "Positive Evaluations")
		aspect(1)
		addplot(kdensity mfgwt if mfgwt >= .04 & mfgwt <= .55, xlab(.05(.05).50) color(gs10) lpattern(dash) yaxis(1) yscale(alt off)
		|| function y = 0.00, range(.05 .50) lwidth(.25) yaxis(2)
		)
		saving(pr_pos, replace)
		;
		#delimit cr		
		
	graph combine pr_neg.gph pr_pos.gph, cols(2) ycommon
	
	graph export "Manufacturing Predictions.pdf", replace



* Predicted Probability - Negative Evals
clear
	set more off
	use "Flynn Fordham II Replication Data Main Models.dta"
	
	nbreg hosteval mfgwt republican college officer cong_dummy2-cong_dummy12, robust

	postutil clear
	tempname mypost
	tempfile results
	postfile `mypost' yhat str20 party str20 variable str20 eval using `results', replace
	
	* NEGATIVE EVALUATION MODELS
		* Republican Base
		margins, at(mfgwt=(.125) republican=(1) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Base") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Base
		margins, at(mfgwt=(.125) republican=(0) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Base") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican College
		margins, at(mfgwt=(.125) republican=(1) college=(1) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("College") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat College
		margins, at(mfgwt=(.125) republican=(0) college=(1) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("College") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican Manufacturing
		margins, at(mfgwt=(.31) republican=(1) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Manufacturing") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Manufacturing
		margins, at(mfgwt=(.31) republican=(0) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Manufacturing") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican Officer
		margins, at(mfgwt=(.125) republican=(1) college=(0) officer=(1) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Officer") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Officer
		margins, at(mfgwt=(.125) republican=(0) college=(0) officer=(1) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Officer") ("Negative Evaluations")
		matrix drop _all
		scalar drop yhat
	
	
	* POSITIVE EVALUATION MODELS
		nbreg faveval mfgwt republican college officer cong_dummy2-cong_dummy12, robust

		* Republican Base
		margins, at(mfgwt=(.125) republican=(1) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Base") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Base
		margins, at(mfgwt=(.125) republican=(0) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Base") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican College
		margins, at(mfgwt=(.125) republican=(1) college=(1) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("College") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat College
		margins, at(mfgwt=(.125) republican=(0) college=(1) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("College") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican Manufacturing
		margins, at(mfgwt=(.31) republican=(1) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Manufacturing") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Manufacturing
		margins, at(mfgwt=(.31) republican=(0) college=(0) officer=(0) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Manufacturing") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		
		* Republican Officer
		margins, at(mfgwt=(.125) republican=(1) college=(0) officer=(1) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Republican") ("Officer") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
		* Democrat Officer
		margins, at(mfgwt=(.125) republican=(0) college=(0) officer=(1) cong_dummy2=0 cong_dummy3=0 cong_dummy4=1 cong_dummy5=0 cong_dummy6=0 cong_dummy7=0 cong_dummy8=0 cong_dummy9=0 cong_dummy10=0 cong_dummy11=0 cong_dummy12=0 ) expression(1-predict(pr(0))) level(95)
		matrix yhat = r(table)
		scalar yhat = yhat[1,1]
		post `mypost' (yhat) ("Democrat") ("Officer") ("Positive Evaluations")
		matrix drop _all
		scalar drop yhat
	
	postclose `mypost'
	use `results', clear
	
	encode eval, gen(eval2)
		
	graph bar yhat, over(party) over(variable, lab(angle(45))) over(eval) ///
	legend(ring(0) pos(1)) ///
	ytitle(Predicted Probability of a Non-Zero Count)
	
	graph export "Predictions by Type.pdf", replace

	
	
	
* EXPORTS GRAPH	
	clear
	import excel "Data on exports.xls", sheet("Sheet1") firstrow case(lower) clear
	
	gen perccotton = (cotton/total)*100
	gen percautomobilesironandsteel = ((automobilesandparts+ironandsteelmillproducts+machinery)/total)*100
	
	label var perccotton "Cotton"
	label var percautomobilesironandsteel "Automobiles, Iron and Steel, and Machinery"
	
	drop if year < 1875
	
	twoway connected perccotton year, lpattern(solid) msym(O) mfcolor(white) ///
	|| connected percautomobilesironandsteel year, lpattern(dash) msym(D) mfcolor(white) ///
	xlab(1875(5)1915) ///
	legend(ring(0) pos(1)) ///
	ytitle(Percent)
	
	graph export "Manufacturing Exports Time.pdf", replace
	
	
	
	
* Total number of evaluations per congress graph and split positive/negative graph
	clear
		set more off
		use "Flynn Fordham II Replication Data Main Models.dta"
		collapse (sum) faveval hosteval, by(congress)
		egen totevals = rowtotal(faveval hosteval)

	* Combined graph
		collapse (sum) faveval hosteval, by(congress)
			egen totevals = rowtotal(faveval hosteval)
			graph bar (sum) totevals, over(congress) legend(off)  ///
			 ytitle(Number of Evaluations) intensity(*.75) ylabel() ///
			 title(Total Number of Threat Evaluations by Congress, size(medium))
	
	* Split graph
			graph bar (sum) faveval hosteval, over(congress) ///
				legend(lab(1 "Positive Evaluations") lab(2 "Negative Evaluations") cols(2) symxsize(3) symysize(3)) ///
				bar(1, color(gs10)) bar(2, color(gs20)) bargap(15) title(Positive and Negative Evaluations by Congress, size(medium)) ///
				ytitle(Number of Evaluations) ylabel()
				
				
* MAP OF MANUFACTURING
* Note: Code will stall once it hits spmap command. Run the entire code, then highlight spmap segment and run just that to generate map.
	clear
	shp2dta using "s_10nv15", database(usdb) coordinates(uscoord) replace
	use usdb.dta
	keep OBJECTID NAME LON LAT Shape_Leng Shape_Area _ID
	rename NAME state
	drop in 57
	sort state
	tempfile map
	save `map', replace
	
	clear
	import excel "Map of manufacturing in 1900.xls", sheet("wide") firstrow
	sort state
	gen OBJECTID = .
	replace OBJECTID = 1 if state == "AK "
	replace OBJECTID = 2 if state == "AL"
	replace OBJECTID = 3 if state == "AR"
	replace OBJECTID = 4 if state == "AMERICAN SAMOA"
	replace OBJECTID = 5 if state == "AZ"
	replace OBJECTID = 6 if state == "CA"
	replace OBJECTID = 7 if state == "CO"
	replace OBJECTID = 8 if state == "CT"
	replace OBJECTID = 9 if state == "DISTRICT OF COLUMBIA"
	replace OBJECTID = 10 if state == "DE"
	replace OBJECTID = 11 if state == "FL"
	replace OBJECTID = 12 if state == "GA"
	replace OBJECTID = 14 if state == "HAWAII"
	replace OBJECTID = 15 if state == "IA"
	replace OBJECTID = 16 if state == "ID"
	replace OBJECTID = 17 if state == "IL"
	replace OBJECTID = 18 if state == "IN"
	replace OBJECTID = 19 if state == "KS"
	replace OBJECTID = 20 if state == "KY"
	replace OBJECTID = 21 if state == "LA"
	replace OBJECTID = 22 if state == "MA"
	replace OBJECTID = 24 if state == "ME"
	replace OBJECTID = 25 if state == "MI"
	replace OBJECTID = 26 if state == "MN"
	replace OBJECTID = 27 if state == "MS"
	replace OBJECTID = 28 if state == "MT"
	replace OBJECTID = 29 if state == "NC"
	replace OBJECTID = 30 if state == "ND"
	replace OBJECTID = 31 if state == "NE"
	replace OBJECTID = 32 if state == "NH"
	replace OBJECTID = 33 if state == "NJ"
	replace OBJECTID = 34 if state == "NM"
	replace OBJECTID = 35 if state == "NV"
	replace OBJECTID = 36 if state == "NY"
	replace OBJECTID = 37 if state == "OH"
	replace OBJECTID = 38 if state == "OK"
	replace OBJECTID = 39 if state == "OR"
	replace OBJECTID = 40 if state == "PA"
	replace OBJECTID = 41 if state == "PUERTO RICO"
	replace OBJECTID = 42 if state == "RI"
	replace OBJECTID = 43 if state == "SD"
	replace OBJECTID = 44 if state == "TN"
	replace OBJECTID = 45 if state == "TX"
	replace OBJECTID = 46 if state == "UT"
	replace OBJECTID = 47 if state == "VA"
	replace OBJECTID = 48 if state == "VIRGIN ISLANDS"
	replace OBJECTID = 49 if state == "VT"
	replace OBJECTID = 50 if state == "WA"
	replace OBJECTID = 51 if state == "WI"
	replace OBJECTID = 52 if state == "WV"
	replace OBJECTID = 53 if state == "WY"
	replace OBJECTID = 54 if state == "MO"
	replace OBJECTID = 55 if state == "SC"
	replace OBJECTID = 65 if state == "GUAM"
	replace OBJECTID = 86 if state == "NORTHERN MARIANAS"
	replace OBJECTID = 198 if state == "MD"
	sort OBJECTID
		
	merge 1:1 OBJECTID using `map', nogen
	drop if mfgpct == .
	format mfgpct %9.2fc
	* Drop Alaska
	drop if OBJECTID == 1
		save `map', replace

	spmap mfgpct using uscoord.dta, id(_ID) fcolor(Greys) ///
	legtitle("Percent of State Workforce") legend(on) legstyle(2) clnumber(7) gsize(3) ///
	title("Manufacturing Employment by State, 1900") xsize(6) ysize(3) ndsize(0.1) mosize(0.1) osize(.1 .1 .1 .1 .1 .1 .1) ///
	label( data(`map') label(mfgpct) xcoord(LON) ycoord(LAT)) 

	graph export "Manufacturing Map.png", width(1000) height(600) replace
	
	
	
	
